package com.xneure.code.log;

import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.xneure.code.log.ILog.Level;
import com.xneure.utils.bean.Caller;

public class DefaultHandle extends LogWatcher{
	private SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
	private Thread timer;
	private String savePath;
	private long logSize = 0;
	private long saveSize = 1024 * 2;
	private long saveTime = 20;
	protected List<String> log = new ArrayList<String>(500);

	public DefaultHandle(){
		initTimeTask();
	}

	public void setSavePath(String savePath){
		this.savePath = savePath;
	}

	protected void initTimeTask(){
		timer = new Thread(new Runnable(){
			public void run(){
				for(;;){
					if(logSize > saveSize){
						saveLog();
					}
					try{
						if(timer.isInterrupted()){
							return;
						}
						synchronized(DefaultHandle.this){
							DefaultHandle.this.wait(saveTime);
						}
					}catch(InterruptedException e){
					}
				}
			}
		});
		timer.setDaemon(true);
	}

	public long getSaveSize(){
		return saveSize;
	}

	public void setSaveSize(long saveSize){
		this.saveSize = saveSize;
	}

	public long getSaveTime(){
		return saveTime;
	}

	public void setSaveTime(long saveTime){
		this.saveTime = saveTime;
	}

	public void saveLog(){
		try{
			synchronized(log){
				if(log.isEmpty()){
					return;
				}
				if(null != getFileName()){
					String tmp = getFileName();
					File file = new File(tmp);
					if(!file.getParentFile().exists()){
						file.getParentFile().mkdirs();
					}
					fos = new FileWriter(file,true);
					for(String str:log){
						fos.append(str);
					}
					log.clear();
					fos.flush();
					fos.close();
					fos = null;
				}
				logSize = 0;
			}
		}catch(Exception e){
		}
	}

	@Override
	public void handle(ILog iLog,Level level,String log){
		Caller caller = null;
		if(null != regex){
			caller = iLog.getCaller();
			if(null == caller){
				return;
			}
			String cls = caller.getClassName();
			// Throwable b1 = new Throwable();
			// try{
			// StringWriter sw = new StringWriter();
			// PrintWriter pw = new PrintWriter(sw);
			// b1.printStackTrace(pw);
			// String t = sw.toString();
			// t.toString();
			// }catch(Exception e){
			// e.printStackTrace();
			// }
			for(String r:regex){
				if(cls.matches(r)){
					int len = 10;
					len += 100 + log.length();
					StringBuilder sb = new StringBuilder(len);
					sb.append(formatter.format(System.currentTimeMillis()));
					sb.append(" ");
					sb.append(level.name());
					sb.append(" ");
					sb.append(caller.getClassName().concat(" 行：").concat(String.valueOf(caller.getLineNumber())));
					sb.append(" ");
					sb.append(log);
					synchronized(this.log){
						String string = sb.toString();
						if(iLog.isSysHandle() == false){
							printLog(level,string);
						}
						if(null != savePath){
							logSize += sb.length();
							this.log.add(string);
						}
					}
					return;
				}
			}
		}
	}

	public long getLogSize(){
		return logSize;
	}

	public void setLogSize(long logSize){
		this.logSize = logSize;
	}

	protected String getFileName(){
		if(null == savePath){
			return null;
		}
		return String.format(savePath,new Date());
	}
}
